package com.miyake.client.tools;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.ComplexPanel;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;
import com.miyake.client.DoctorsUserService;
import com.miyake.client.DoctorsUserServiceAsync;

abstract public class ImageList extends Composite {
	public interface CommunityListHandler {
		void onSelect(ImageListElement community);
		void onFind();
		void onCreate(String name);
		void onDelete(String name);
	}
	private List<CommunityListHandler> handlers = new ArrayList<CommunityListHandler>();
	public void addCommunityListHandler(CommunityListHandler handler) {
		handlers.add(handler);
	}
	protected void fireSelectEvent(ImageListElement community) {
		for (CommunityListHandler handler : handlers) {
			handler.onSelect(community);
		}
	}
	protected void fireFindEvent() {
		for (CommunityListHandler handler : handlers) {
			handler.onFind();
		}
	}
	
	protected DoctorsUserServiceAsync service = DoctorsUserService.Util.getInstance();
	private VerticalPanel contentPanel;
	
	abstract protected String getCommunityTitle();
	abstract protected void getList();
	abstract protected boolean canCreate();
	abstract protected boolean canFind();
	abstract protected void createCommuniy(String text, final VerticalPanel verticalPanel);
	protected Widget getCustomHorizontalWidget(ImageListElement prop){return null;}
	protected Widget getCustomVerticalWidget(ImageListElement prop){return null;}
	public ImageList() {
		final VerticalPanel verticalPanel = new VerticalPanel();
		initWidget(verticalPanel);
		verticalPanel.setWidth("100%");
		//
		verticalPanel.add(new HTML("<h2>"+ getCommunityTitle()+ "</h2>"));
		//
		contentPanel = new VerticalPanel();
		verticalPanel.add(contentPanel);
		
		HorizontalPanel toolBar = new HorizontalPanel();
		verticalPanel.add(toolBar);
		if (canCreate()) {
			Button create = new SmallButton("Create");
			toolBar.add(create);
			create.addClickHandler(new ClickHandler() {
				@Override
				public void onClick(ClickEvent event) {
					new ShowDialog() {
						@Override
						protected void onOK(String text) {
							createCommuniy(text, contentPanel);
						}

						@Override
						protected DialogBox getDialogBox() {
							return new TextDialog();
						}
					};
				}
			});
		}
		if (canFind()) {
			Button find = new SmallButton("Find", new ClickHandler() {
				@Override
				public void onClick(ClickEvent event) {
					fireFindEvent();
				}
			});
			toolBar.add(find);
		}	
		
		//
		getList();
		//
	}
		
	protected void showList(List<ImageListElement> result, int columns) {
		contentPanel.clear();
		
		Grid grid = new Grid(1, columns);
		int rows = result.size() / columns + 1;
		grid.resize(rows, columns);
		Iterator<ImageListElement> it = result.iterator();
		for (int row = 0; row < rows; row++) {
			for (int col = 0; col < columns; col++) {
				if (it.hasNext()) {
					grid.setWidget(row, col, getWidget(it.next()));
				}
			}
		}
		contentPanel.add(grid);
		grid.setWidth("100%");
	}

	protected ComplexPanel getWidget(final ImageListElement prop) {
	
		VerticalPanel p = new VerticalPanel();
		Image image = new ClickableImage("FaceSample.PNG"/*prop.getImageURL()*/);
		image.setSize("60px", "60px");
		p.add(image);
		Label label = new ClickableLabel(prop.getName());
		p.add(label);
		p.setCellHorizontalAlignment(label, HasHorizontalAlignment.ALIGN_CENTER);
		ClickHandler handler = new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				fireSelectEvent(prop);
			}
		};
		label.addClickHandler(handler);
		image.addClickHandler(handler);
		
		HorizontalPanel h = null;
		if (getCustomHorizontalWidget(prop) != null) {
			h = new HorizontalPanel();
			h.add(p);
			h.add(getCustomHorizontalWidget(prop));
		}
		VerticalPanel v = null;
		if (getCustomVerticalWidget(prop) != null) {
			v = new VerticalPanel();
			if (h != null) {
				v.add(h);
			}
			else {
				v.add(p);
			}
			v.add(getCustomVerticalWidget(prop));
		}
		
		if (v != null) {
			return v;
		}
		if (h != null) {
			return h;
		}
		return p;
	}

}
